package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Address> addressesList = this.addressService.pageQuery();
        if (addressesList != null) {
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, addressesList);
        }
        return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
    }

    /**
     * 新增
     *
     * @return
     */
    @PostMapping("/submit")
    public Result add(@RequestBody Address address) {

        int bool = this.addressService.add(address);
        if (bool == 1) {
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);

    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Address address = addressService.findById(id);
        if (address != null) {
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS, address);
        }
        return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {

        Boolean bool = addressService.delete(id);
        if (bool) {
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
    }

    /**
     * 是否启用
     * @param id
     * @param enable
     * @return
     */
    @PutMapping("/{id}/{enable}")
    private Result updeteEnable(@PathVariable("id") Integer id, @PathVariable("enable") Integer enable) {
        addressService.updateEnable(id, enable);
        return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
    }


}
